<?php
namespace app\user\controller;
use think\Request;
use app\common\controller\Common;
class Repair extends Common
{
	protected $model = null;
	protected $keyword_search = [];
	protected $order = 'repair_id desc';

	public function _initialize()
	{
		parent::_initialize();
        $this->model = model('repair/Repair');
	}
	
	/**
	 * 客户维修管理
	 */
	public function index()
	{
		if(Request()->isGet()){
			if(Request()->isAjax()){
				$data = Request()->param('');

				$page = isset($data['page']) ? $data['page'] : 1;
				$row = isset($data['limit']) ? $data['limit'] : $this->row;
				$search_arr = isset($data['keyword']) ? $data['keyword'] : '';
				$search = $this->keyword_search($search_arr);
				$order = $this->order;
				$where = $this->handleList($data);
				
				$list = $this->model->listData($where, $order, $page, $row, $search);
				$res['list'] = collection($list)->toArray();
				$count = $this->model->listCount($where, $order, $search);
				$money = $this->model->countRepairMoney($where, $order, $search);
				$res['count'] = ['count'=>$count,'money'=>$money];
				return $this->jsonList(0, config('status.SUCCESS_ONE'), $count, $res, $page, $row);
			}
			return $this->fetch('');
		}
	}
	/**
	 * 客户维修记录
	 */
	public function repair_log()
	{
		if(Request()->isGet()){
			return $this->fetch('');
		}
	}
	/**
	 * 维修单详情
	 * @param intger $ids 维修单id
	 */
	public function repair_details($ids = null)
	{
		$row = model('repair/Repair')->get($ids);
		if(Request()->isGet()){
			$this->assign('row',$row);
			return $this->fetch('');
		}
	}
	/**
	 * 列表查询条件处理
	 * @param array $data 查询参数
	 */
	protected function handleList($data)
	{
		$where = '';
		/*@付款并取机*/
		$where = "`state` = 8";
		/*@用户编号*/
		if(isset($data['user_sn']) && $data['user_sn'] != ''){
			$str = '`user_sn` = "' . $data['user_sn'] . '"';
			$where .= $where == '' ? $str : ' and ' . $str;
		}
		/*@登陆者判断*/
		/*店长*/
		if(session('admin.admin_type') == 1){
			$str = '`shop_id` = ' . session('admin.shop_id');
			$where .= $where == '' ? $str : ' and ' . $str;
		}
		/*营业员*/
		if(session('admin.admin_type') == 2){
			$str = '`admin_id` = ' . session('admin.admin_id');
			$where .= $where == '' ? $str : ' and ' . $str;
		}
		/*维修员*/
		if(session('admin.admin_type') == 3){
			// $str = "(`shop_id` = ". session('admin.shop_id') . " and `state` = 3) or (`state` in ('4,8,9,10') and `technician_id` = " . session('admin.admin_id') . ')';
			$str = "`technician_id` = " . session('admin.admin_id');
			$where .= $where == '' ? $str : ' and ' . $str;
		}
		/*@时间*/
		if(isset($data['start']) && $data['start'] != '' && isset($data['end']) && $data['end'] != ''){
			if(strtotime($data['start']) > strtotime($data['end'])){
				return false;
			}
			$str = '`addtime` >= ' . strtotime($data['start']) . ' and `addtime` < ' . strtotime($data['end']);
			$where .= $where == '' ? $str : ' and ' . $str;
		}else if(isset($data['start']) && $data['start'] != ''){
			$str = '`addtime` >= ' . strtotime($data['start']);
			$where .= $where == '' ? $str : ' and ' . $str;
		}else if(isset($data['end']) && $data['end'] != ''){
			$str = '`addtime` < ' . strtotime($data['end']);
			$where .= $where == '' ? $str : ' and ' . $str;
		}
		return $where;
	}
}